package com.liuguang.gateway.config.quartz;

import cn.hutool.core.date.DateUtil;
import com.liuguang.gateway.config.quartz.job.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: JobRunner
 * @Description：
 * @Author: wanghr
 * Date: 6:45 PM 2020/9/24
 * Version: 1.0
 */
//@Component
public class JobRunner implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(JobRunner.class);


    /**
     * 容器启动后调用 定时任务
     *
     * @params: [args]
     * @return: void
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("开始执行定时任务---------- " + args);
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        JobDetail jobDetail = JobBuilder
                .newJob(MyJob.class)
                .withDescription("这是我自定义的任务")
                .withIdentity("任务1", "第一小组")
                .build();

        SimpleTrigger trigger = TriggerBuilder
                .newTrigger()
//                .withIdentity("自定义的触发")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(4).repeatForever())
                .build();

        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
        //测试当前线程睡眠
        logger.info("线程：{}，当前时间：{}", Thread.currentThread().getName(), DateUtil.now());
        TimeUnit.SECONDS.sleep(1);
        logger.info("线程：{}，当前时间：{}", Thread.currentThread().getName(), DateUtil.now());
    }
}
